package xps.and.uudaijia.userclient.view.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.zhy.autolayout.AutoLinearLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import xps.and.uudaijia.userclient.R;
import xps.and.uudaijia.userclient.SPConstants;
import xps.and.uudaijia.userclient.util.AppCommond;
import xps.and.uudaijia.userclient.view.MainActivity;
import xps.and.uudaijia.userclient.view.base.BaseActivity;

/**
 * Created by Administrator on 2017/7/24.
 */

public class SetUpActivity extends BaseActivity {
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.actionbar_tv_title)
    TextView actionbarTvTitle;
    @BindView(R.id.Modify_Layout)
    AutoLinearLayout ModifyLayout;
    @BindView(R.id.Sign_in)
    Button SignIn;
    @BindView(R.id.FW_Layout)
    AutoLinearLayout FWLayout;
    @BindView(R.id.ME_Layout)
    AutoLinearLayout MELayout;
    @BindView(R.id.KF_Layout)
    AutoLinearLayout KFLayout;
    @BindView(R.id.YQ_Layout)
    AutoLinearLayout YQLayout;
    @BindView(R.id.YYBBKG_Img)
    ImageView YYBBKGImg;

    @Override
    protected int getLayout() {
        return R.layout.activity_setup;
    }

    @Override
    protected void setData() {

    }

    @Override
    protected void init() {
        ButterKnife.bind(this);
        actionbarTvTitle.setText("账号设置");
        if (getVBEnable()) {
            YYBBKGImg.setImageResource(R.drawable.ios7_switch_on);
        } else {
            YYBBKGImg.setImageResource(R.drawable.ios7_switch_off);
        }
    }

    @OnClick({R.id.actionbar_iv_back, R.id.YYBBKG_Img, R.id.FWXY_Layout, R.id.YQ_Layout, R.id.actionbar_tv_title, R.id.ME_Layout, R.id.KF_Layout, R.id.Sign_in, R.id.actionbar_al, R.id.Modify_Layout, R.id.FW_Layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                finish();
                break;
            case R.id.Sign_in:
                SharedPreferences sp = JUtils.getSharedPreference();
                SharedPreferences.Editor spEd = sp.edit();
                spEd.putString("User_Access_token", "");
                spEd.putString("User_ID", "");
                spEd.putString("User_Name", "");
                spEd.putString("User_Phone", "");
                spEd.putString("User_Img", "");
                spEd.putString("YHJ", "");
                spEd.putBoolean("User_Login", false);
                spEd.commit();
                Intent Logintent = new Intent(SetUpActivity.this, LoginActivity.class);
                Logintent.putExtra("zhanghao", "2");
                startActivity(Logintent);
                JPushInterface.setAliasAndTags(SetUpActivity.this, "", null, mAliasCallback);
                finish();
                AppCommond appCommond = new AppCommond.Builder()
                        .setOpration(AppCommond.CLOSE_ACTIVITY)
                        .setRequestcloseAcname(PersonActivity.class.getSimpleName())
                        .build();
                EventBus.getDefault().post(appCommond);

                AppCommond appCommond1 = new AppCommond.Builder()
                        .setOpration(AppCommond.CLOSE_ACTIVITY)
                        .setRequestcloseAcname(MainActivity.class.getSimpleName())
                        .build();
                EventBus.getDefault().post(appCommond1);
                break;
            case R.id.FW_Layout:
                Intent intent = new Intent(SetUpActivity.this, ChargeActivity.class);
                startActivity(intent);
                break;
            case R.id.ME_Layout:
                Intent Meintent = new Intent(SetUpActivity.this, MeActivity.class);
                startActivity(Meintent);
                break;
            case R.id.KF_Layout:
                showCustomizeDialog();
                break;
            case R.id.FWXY_Layout:
                Intent XYintent = new Intent(SetUpActivity.this, XYActivity.class);
                startActivity(XYintent);
                break;
            case R.id.YQ_Layout:
                Intent Invitationintent = new Intent(SetUpActivity.this, InvitationActivity.class);
                startActivity(Invitationintent);
                break;
            case R.id.Modify_Layout:
                Intent Modifyintent = new Intent(SetUpActivity.this, ModifyFKActivity.class);
                startActivity(Modifyintent);
                break;
            case R.id.YYBBKG_Img:
                boolean state = !getVBEnable();
                JUtils.getSharedPreference().edit()
                        .putBoolean(SPConstants.VOICE_ENABLE, state)
                        .commit();
                if (state) {
                    YYBBKGImg.setImageResource(R.drawable.ios7_switch_on);
                } else {
                    YYBBKGImg.setImageResource(R.drawable.ios7_switch_off);
                }
                break;

        }
    }

    /**
     * 返回语音播报开启状态
     * s
     *
     * @return 状态
     */
    boolean getVBEnable() {
        boolean state = false;
        try {
            state = JUtils.getSharedPreference().getBoolean(SPConstants.VOICE_ENABLE, false);
            return state;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return state;
    }

    private void showCustomizeDialog() {
    /* @setView 装入自定义View ==> R.layout.dialog_customize
     * 由于dialog_customize.xml只放置了一个EditView，因此和图8一样
     * dialog_customize.xml可自定义更复杂的View
     */
        AlertDialog.Builder customizeDialog =
                new AlertDialog.Builder(SetUpActivity.this);
        final View dialogView = LayoutInflater.from(SetUpActivity.this)
                .inflate(R.layout.dialog_dianhua, null);
        customizeDialog.setTitle("联系客服");
        customizeDialog.setView(dialogView);
        customizeDialog.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        customizeDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 获取EditView中的输入内容
                        Intent phoneIntent = new Intent("android.intent.action.CALL",
                                Uri.parse("tel: 022-58397002"));
                        startActivity(phoneIntent);
                    }
                });
        customizeDialog.show();
    }

    private final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            String logs;
            switch (code) {
                case 0:
                    logs = "Set tag and alias success极光推送别名设置成功";
                    Log.i("mAliasCallback", logs);
                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.极光推送别名设置失败，60秒后重试";
                    Log.i("mAliasCallback", logs);
                    break;
                default:
                    logs = "极光推送设置失败，Failed with errorCode = " + code;
                    Log.e("mAliasCallback", logs);
                    break;
            }
        }
    };
}
